package com.entfrm.core.base.util;

import cn.hutool.core.thread.ThreadUtil;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author kyc
 * 2020-06-23
 * exchange企业邮箱工具
 */
@Component
public class ExchangeMailUtil {


    /**
     * 创建线程池
     **/
//    static ExecutorService fixedThreadPool = Executors.newCachedThreadPool();

    public void informToOn(){
        ThreadUtil.execute(new Runnable() {
            @Override
            public void run() {

            }
        });
    }
    /**
     * 通知上级领导
     * @param repairOrder 报修单
     **/
    /*public static void informToOn(RepairOrder repairOrder) {
        UserService userService = SpringUtils.getBean(UserService.class);
        fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    if (Constants.Value.Zero.equals(reList.getState())) {
                        // 新增报修单(多久时间未处理通知经理)
                        Thread.sleep(30000);
                        ReList r = reListService.getById(reList.getId());
                        if (Constants.Value.Zero.equals(r.getState())){
                            // todo 发送邮件到经理
                            System.out.println("新单超时处理（经理）");
                            Superuser superuser = superuserService.getByType(1);
                            List<String> recipientTo = new ArrayList<>();
                            recipientTo.add(superuser.getSuperuserEmail());
                            re = new ReadConfigUtil("application.properties");
                            mailService.sendMail(re.getValue("exchange.account"), re.getValue("exchange.password"),
                                    "chery",re.getValue("exchange.emailAddress"), "工单超时未分配",
                                    recipientTo, "有新的工单超时未指派处理!");
                        }
                    } else {
                        // 指派报修单(多久时间未处理通知经理)
                        Thread.sleep(40000);
                        ReList r = reListService.getById(reList.getId());
                        if (!r.getState().equals(Constants.Value.TWO)){
                            // todo 发送邮件到经理
                            System.out.println("新单超时解决（经理）");
                            Superuser superuser = superuserService.getByType(1);
                            List<String> recipientTo = new ArrayList<>();
                            re = new ReadConfigUtil("application.properties");
                            mailService.sendMail(re.getValue("exchange.account"), re.getValue("exchange.password"),
                                    "chery", re.getValue("exchange.emailAddress"), "工单超时未解决", recipientTo, "有工单超时未解决!");
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    if (reList.getState().equals(Constants.Value.Zero)) {
                        // 新增报修单(多久时间未处理通知总监)
                        Thread.sleep(60000);
                        ReList r = reListService.getById(reList.getId());
                        if (r.getState().equals(Constants.Value.Zero)){
                            //  发送邮件到总监
                            System.out.println("新单超时处理（总监）");
                            Superuser superuser = superuserService.getByType(2);
                            List<String> recipientTo = new ArrayList<>();
                            recipientTo.add(superuser.getSuperuserEmail());
                            re = new ReadConfigUtil("application.properties");
                            mailService.sendMail(re.getValue("exchange.account"), re.getValue("exchange.password"),
                                    "chery", re.getValue("exchange.emailAddress"), "工单超时未分配", recipientTo, "有新的工单超时未指派处理!");
                        }
                    } else {
                        // 指派报修单(多久时间未处理通知总监)
                        Thread.sleep(80000);
                        ReList r = reListService.getById(reList.getId());
                        if (!r.getState().equals(Constants.Value.TWO)){
                            // 发送邮件到总监
                            System.out.println("新单超时解决（总监）");
                            Superuser superuser = superuserService.getByType(2);
                            List<String> recipientTo = new ArrayList<>();
                            recipientTo.add(superuser.getSuperuserEmail());
                            re = new ReadConfigUtil("application.properties");
                            mailService.sendMail(re.getValue("exchange.account"), re.getValue("exchange.password"),
                                    "chery", re.getValue("exchange.emailAddress"), "工单超时未解决", recipientTo, "有工单超时未解决!");
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }*/

    /**
     * 发送邮件(无附件抄送人)
     *
     * @param account      登录账户
     * @param password     登陆密码
     * @param hostname     服务地址
     * @param subject      标题
     * @param recipientTo  收件人
     * @param message      邮件消息内容
     * @return boolean
     */
    @Async
    public void sendMail(String account, String password, String hostname, String subject,
                         List<String> recipientTo, String message) {
        ExchangeClient exchangeClient = new ExchangeClient.ExchangeClientBuilder()
                .hostname(hostname)
                .exchangeVersion(ExchangeVersion.Exchange2010)
                .username(account)
                .password(password)
                .subject(subject)
                .message(message)
                .recipientTo(recipientTo)
                .build();
        exchangeClient.sendExchange();
    }

    /**
     * 发送邮件(有附件抄送人)
     *
     * @param recipientCc  抄送
     * @param recipientBcc 密抄
     * @param attachments  附件
     * @param account      登录账户
     * @param password     登陆密码
     * @param hostname     服务地址
     * @param subject      标题
     * @param recipientTo  收件人
     * @param message      邮件消息内容
     * @return boolean
     */
    @Async
    public void sendMail(String account, String password, String hostname, String subject,
                                List<String> recipientTo,List<String> recipientCc,
                                List<String> recipientBcc, String message,String attachments) {
        ExchangeClient exchangeClient = new ExchangeClient.ExchangeClientBuilder()
                .hostname(hostname)
                .exchangeVersion(ExchangeVersion.Exchange2010)
                .username(account)
                .password(password)
                .subject(subject)
                .message(message)
                .recipientTo(recipientTo)
                .recipientCc(recipientCc)
                .recipientBcc(recipientBcc)
                .attachments(attachments)
                .build();
        exchangeClient.sendExchange();
    }


}
